import traceback

from commons.assert_util import AssertUtil
from commons.extract_util import ExtractUtil
from commons.model_util import CaseInfo
from commons.request_util import RequestUtil, logger

eu = ExtractUtil()
re = RequestUtil()
au = AssertUtil()
def stand_case_flow(case_obj:CaseInfo):
    # 把请求参数写入日志
    logger.info("模块-> " + str(case_obj.feature) + "\t接口-> " + str(case_obj.story) + "\t用例-> " + str(case_obj.title))
    # 使用提取的值
    new_request = eu.use_extract_value(case_obj.request)
    # 发送请求
    res = re.send_all_request(**new_request)
    # 请求之后得到响应后去提取变量
    if case_obj.extract:
        for key, value in case_obj.extract.items():
            eu.extract(res, key, *value)  # *value 为一个列表 [json,"$.data.id"]
    print(res.json())
    try:
        # 请求之后得到响应，如果validate不为None，则需断言
        if case_obj.validate:
            for assert_type, value in  eu.use_extract_value(case_obj.validate).items():
                au.assert_all_case(res, assert_type, value)
            logger.info("断言成功!" + "\n")
        else:
            logger.warning("此用例没有断言！" + "\n")
    except Exception as e:
        logger.error("断言失败! %s" % str(traceback.format_exc()))
        raise e
